package servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import managers.AffichageManager;
import beans.Commande;
import beans.User;
import security.CheckCreditCard;
import security.DateValide;

/**
 * Servlet implementation class ConfirmerPaiement
 */
public class ConfirmerPaiementServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ConfirmerPaiementServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Commande panier = new Commande();
		panier =  (Commande) request.getSession().getAttribute("panier");
		float totalwithfdp = ((float) ((int) ((panier.getTotal() + 10)*100))) / 100;
		session.setAttribute("testnumerocarte", false);
		session.setAttribute("testcryptogramme", false);
		session.setAttribute("testdateexpiration", false);
		
		if (CheckCreditCard.checkCreditCard(request.getParameter("numerocarte")) == false)
		{
			session.setAttribute("testnumerocarte", true);
			request.setAttribute("affichage", AffichageManager
					.buildAffichagePaiement((User) request.getSession().getAttribute(
							"user"), (Commande) request.getSession().getAttribute("panier")));
			request.getRequestDispatcher("/paiement.jsp").forward(request, response);
		}
		else if (request.getParameter("cryptogramme") == null || request.getParameter("cryptogramme").toString().length() >= 4)
		{
			session.setAttribute("testcryptogramme", true);
			request.setAttribute("affichage", AffichageManager
					.buildAffichagePaiement((User) request.getSession().getAttribute(
							"user"), (Commande) request.getSession().getAttribute("panier")));
			request.getRequestDispatcher("/paiement.jsp").forward(request, response);
		}
		else if (DateValide.afterNow(Integer.parseInt(request.getParameter("annee")), Integer.parseInt(request.getParameter("mois"))) == false)
		{
			session.setAttribute("testdateexpiration", true);
			request.setAttribute("affichage", AffichageManager
					.buildAffichagePaiement((User) request.getSession().getAttribute(
							"user"), (Commande) request.getSession().getAttribute("panier")));
			request.getRequestDispatcher("/paiement.jsp").forward(request, response);
		}
		else
		{
			session.setAttribute("testnumerocarte", false);
			session.setAttribute("testcryptogramme", false);
			session.setAttribute("testdateexpiration", false);
			request.setAttribute("affichage", AffichageManager
					.buildAffichageConfirmerPaiement((User) request.getSession().getAttribute(
							"user"), (Commande) request.getSession().getAttribute("panier"),
							totalwithfdp, request.getParameter("numerocarte").substring(
									request.getParameter("numerocarte").length() -4, 
									request.getParameter("numerocarte").length()),
							(Integer.parseInt(request.getParameter("mois"))+1)+"/"+request.getParameter("annee"),
							request.getParameter("nomprenom")));
			request.getRequestDispatcher("/confirmCommande.jsp").forward(request, response);
		}
		
	}

}
